package org.myspringmvc.util;

import lombok.SneakyThrows;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * @author 莫磊
 * @date 2025/8/14
 * @description
 */
public class ObjectUtil {

    public static boolean isBaseType(Class<?> clazz) {
        if (clazz.isPrimitive()
                || String.class.isAssignableFrom(clazz)
                || Number.class.isAssignableFrom(clazz)
                || Map.class.isAssignableFrom(clazz)
                || List.class.isAssignableFrom(clazz)) {
            return true;
        } else {
            return false;
        }
    }

    public static Object convertParam(Object attribute, Class<?> type) {
        if (type == String.class) {
            return (String) attribute;
        } else if (type == int.class) {
            return Integer.parseInt((String) attribute);
        } else if (type == Integer.class) {
            return Integer.valueOf((String) attribute);
        } else if (type == long.class) {
            return Long.parseLong((String) attribute);
        } else if (type == Long.class) {
            return Long.valueOf((String) attribute);
        } else if (type == double.class) {
            return Double.parseDouble((String) attribute);
        } else if (type == Double.class) {
            return Double.valueOf((String) attribute);
        } else if (type == float.class) {
            return Float.parseFloat((String) attribute);
        } else if (type == Float.class) {
            return Float.valueOf((String) attribute);
        } else if (type == boolean.class) {
            return Boolean.parseBoolean((String) attribute);
        } else if (type == Boolean.class) {
            return Boolean.valueOf((String) attribute);
        } else {
            return null;
        }
    }


    /**
     * 根据配置的名称和值设置属性
     * @param obj
     * @param attributeName
     * @param attributeValue
     */
    @SneakyThrows
    public static  void setFieldForBean(Object obj, String attributeName, String attributeValue) {
        // 根据名称获取set方法
        String methodName = "set" + attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1);
        Class<?> clazz = obj.getClass();
        // 获取对应参数的类型
        Class<?> paramType = clazz.getDeclaredField(attributeName).getType();
        // 获取对应方法
        Method declaredMethod = clazz.getDeclaredMethod(methodName, paramType);
        // 调用方法设置值，暂时制作基础类型的参数处理
        Object paramValue = convertParamType(attributeValue, paramType);
        declaredMethod.invoke(obj, paramValue);
    }

    public static Object convertParamType(String attributeValue, Class<?> paramType) {
        if (paramType == String.class) {
            return attributeValue;
        } else if (paramType == int.class) {
            return Integer.parseInt(attributeValue);
        } else if (paramType == Integer.class) {
            return Integer.valueOf(attributeValue);
        } else if (paramType == long.class) {
            return Long.parseLong(attributeValue);
        } else if (paramType == Long.class) {
            return Long.valueOf(attributeValue);
        } else if (paramType == double.class) {
            return Double.parseDouble(attributeValue);
        } else if (paramType == Double.class) {
            return Double.valueOf(attributeValue);
        } else if (paramType == float.class) {
            return Float.parseFloat(attributeValue);
        } else if (paramType == Float.class) {
            return Float.valueOf(attributeValue);
        } else if (paramType == boolean.class) {
            return Boolean.parseBoolean(attributeValue);
        } else if (paramType == Boolean.class) {
            return Boolean.valueOf(attributeValue);
        } else {
            return null;
        }
    }
}
